﻿using Microsoft.EntityFrameworkCore;
using PublicFunction;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using XinjeMesProgram.Entities.BookInfo;

namespace XinjeMesProgram.Entities.MesAPIServices
{
    public class MesServices
    {
        private readonly CancellationTokenSource stopWork;

        public Dictionary<string, int> UserIsLocked { get; set; }

        public List<BookingListInfoModel> BookingListInfos { get; set; }
        public MesServices()
        {
            this.stopWork = new CancellationTokenSource();
            this.UserIsLocked = new Dictionary<string, int>();
            this.BookingListInfos = new List<BookingListInfoModel>();
        }

        public bool MainTask()
        {
            Task userIsLockedTask = new Task(() =>
            {
                this.GetUserIsLocked(this.stopWork.Token);
            });
            userIsLockedTask.Start();

            //Task book = new Task(() =>
            //{
            //    this.GetBooking3DaysInfo(this.stopWork.Token);
            //});
            //book.Start();
            return true;
        }

        private void GetUserIsLocked(CancellationToken stopMark)
        {
            while (true)
            {
                Thread.Sleep(1);
                if (stopMark.IsCancellationRequested)
                {
                    break;
                }
                try
                {
                    using (FactoryMesContext factoryMes = new FactoryMesContext())
                    {
                        var info = from v in factoryMes.MesUserRole
                                   select new { v.ID, v.IsLocked };
                        var query = info.ToList();
                        foreach (var detail in query)
                        {
                            if (this.UserIsLocked.ContainsKey(detail.ID))
                            {
                                this.UserIsLocked[detail.ID] = (int)detail.IsLocked;
                            }
                            else
                            {
                                this.UserIsLocked.Add(detail.ID, (int)detail.IsLocked);
                            }
                        }
                    }
                }
                catch (Exception ex)
                {
                    MyLogFunction.SaveSysEventLog("获取用户数据信息", ex.Message);
                }
                Thread.Sleep(10 * 1000);
            }
        }

        private void GetBooking3DaysInfo(CancellationToken stopMark)
        {
            while (true)
            {
                Thread.Sleep(1);
                if (stopMark.IsCancellationRequested)
                {
                    break;
                }
                try
                {
                    using (FactoryMesContext factoryMes = new FactoryMesContext())
                    {
                        string sql = @"select * from BookingListInfo where ID  in(
                                       Select min(ID) as id FROM BookingListInfo where CONVERT (varchar(10),BookingTime,120)<=CONVERT (varchar(10),GETDATE(),120)
                                       and CONVERT (varchar(10),BookingTime,120)>=CONVERT (varchar(10),GETDATE()-30,120) and BookingDeleteStatus = 0 
                                       group by BookinglistNo ) order by ID desc";
                        this.BookingListInfos = factoryMes.BookingListInfo.FromSqlRaw(sql).ToList();
                    }
                }
                catch (Exception ex)
                {
                    MyLogFunction.SaveSysEventLog("获取发货数据数据信息", ex.Message);
                }
                Thread.Sleep(5 * 1000);
            }
        }
    }
}

